package cn.edu.scau.cmi.chenyipeng.spring.dao;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.Example;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import cn.edu.scau.cmi.chenyipeng.spring.util.ScauCmiHibernateSessionFactoryUtil;
import cn.edu.scau.cmi.chenyipeng.spring.domain.Student;
import cn.edu.scau.cmi.chenyipeng.spring.domain.Teacher;

public class StudentDAO {
	private static final Logger log = LoggerFactory.getLogger(StudentDAO.class);
	public static final String NAME = "name";
	private Session session;
	
	public Session getSession() {
		session=ScauCmiHibernateSessionFactoryUtil.getSession();
		return session;
	}
	
	public void save(Student transientInstance) {
		log.debug("saving Student instance");
		try {
//			如果
			getSession();
			Transaction transaction = session.beginTransaction();
			session.save(transientInstance);
			log.debug("save Student instance successful");
			transaction.commit();
		} catch (RuntimeException re) {
			log.error("save failed");
			throw re;
		}
	}
	
	public void delete(Student persistentInstance) {
		log.debug("deleting Student instance");
		try {
			getSession();
			Transaction transaction = session.beginTransaction();
			session.delete(persistentInstance);
			log.debug("delete successful");
			transaction.commit();
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			//throw re;
		}
	}
	
	public void update(Student persistentInstance) {
		log.debug("updating Student instance");
		try {
			getSession();
			Transaction transaction = session.beginTransaction();
			session.update(persistentInstance);
			log.debug("update successful");
			transaction.commit();
		} catch(RuntimeException re) {
			log.error("update failed", re);
			//throw re;
		}
	}
	
	public void setTeacher(Student student, Teacher teacher) {
		log.debug("setting student's teacher");
		try {
			getSession();
			Transaction transaction = session.beginTransaction();
			Teacher temp = (Teacher)session.get(Teacher.class, teacher.getId());
			if(temp != null) {
				student.setTeacher(teacher);
				session.update(student);
				log.debug("set student's teacher successful");
				transaction.commit();
			}
			
		}catch(RuntimeException re) {
			log.error("update failed", re);
			//throw re;
		}
		
	}
	
	public List findAll() {
		log.debug("finding all student");
		try {
			String queryStr = "from Student";
			Query queryObject = getSession().createQuery(queryStr);
			log.debug("find by property successful");
			return queryObject.list();
		}
		catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
		
	}
	
	public Student findById(java.lang.Long id) {
		log.debug("getting Student instance with id: " + id);
		try {
			Student instance = (Student) getSession().get("cn.edu.scau.cmi.chenyipeng.hibernate.Student", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	public List findByExample(Student instance) {
		log.debug("finding Student instance by example");
		try {
			List results = getSession().createCriteria("cn.edu.scau.cmi.chenyipeng.hibernate.Student")
					.add(Example.create(instance)).list();
			log.debug("find by example successful, result size: " + results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	public List findByProperty(String propertyName, Object value) {
		log.debug("finding Student instance with property: " + propertyName + ", value: " + value);
		try {
			String queryString = "from Student as model where model." + propertyName + "= ?";
			Query queryObject = getSession().createQuery(queryString);
			queryObject.setParameter(0, value);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	public List findByName(Object name) {
		return findByProperty(NAME, name);
	}
	
	
}
